package com.example.bitmaploading.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.AnimationDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.bitmaploading.BaseActivity;
import com.example.bitmaploading.R;
import com.example.bitmaploading.utils.ImageUtils;
import com.example.bitmaploading.utils.LogUtils;

/**
 * 
 * @author HAYUKLEUNG
 *
 */
public class MainActivity extends BaseActivity implements OnClickListener
{
	/** widgets */
	private ImageView imgDisplay;
	private Button btnError;
	private Button btnCorrect;
	private TextView txtChat;
	
	/** constants */
	private static final int LOADING_METHOD_ERROR = 1;
	private static final int LOADING_METHOD_CORRECT = 2;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initWidgets();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	/** initialize widgets */
	private void initWidgets()
	{
		imgDisplay = (ImageView) this.findViewById(R.id.ActivityMain$img_display);
		btnError = (Button) this.findViewById(R.id.ActivityMain$btn_error);
		btnCorrect = (Button) this.findViewById(R.id.ActivityMain$btn_correct);
		txtChat = (TextView) this.findViewById(R.id.ActivityMain$txt_chat);
		// add listener
		btnError.setOnClickListener(this);
		btnCorrect.setOnClickListener(this);
	}

	@Override
  public void onClick(View v)
  {
	  switch (v.getId())
    {
		case R.id.ActivityMain$btn_error:
			(new BitmapLoadingTask()).execute(LOADING_METHOD_ERROR);
			break;
		case R.id.ActivityMain$btn_correct:
			(new BitmapLoadingTask()).execute(LOADING_METHOD_CORRECT);
			break;
		default:
			break;
		}
  }
	
	/**
	 * 
	 * @author HAYUKLEUNG
	 *
	 */
	private class BitmapLoadingTask extends AsyncTask<Integer, Integer, Bitmap>
	{
		private final int RES_ID = R.drawable.img_display_new_building;
		/*
		// 单例
	  private static BitmapLoadingTask instance = null;
	  
	  private static class SingletonHolder {
	    private static BitmapLoadingTask instance = new BitmapLoadingTask();
	  }
	  
	  // 构造函数 - 单例
	  private BitmapLoadingTask()
	  {
	  }
	  
	  // 单例
	  public static BitmapLoadingTask getInstance()
	  {
	    if (instance == null)
	    {
	      instance = new BitmapLoadingTask();
	    }
	    return instance;
	  }
		*/
		@Override
    protected void onPreExecute()
    {
	    super.onPreExecute();
	    // get bitmap from image view
	    //imgDisplay.setDrawingCacheEnabled(true);
	    //Bitmap bitmap = imgDisplay.getDrawingCache(false);
	    //imgDisplay.setDrawingCacheEnabled(false);
	    // 将 ImageView 的图像清空，保证 Android 系统自动释放 Bitmap 占用的内存
	    imgDisplay.setImageBitmap(null);
	    // 显示 loading 动画
	    imgDisplay.setImageResource(R.anim.bubble);
	    AnimationDrawable animationDrawable = (AnimationDrawable) imgDisplay.getDrawable();
	    animationDrawable.start();

    }

		@Override
		protected Bitmap doInBackground(Integer... params)
		{
			// 注意 doInBackground() 不允许执行 UI 操作
			Bitmap bitmap = null;
			switch (params[0])
			{
			case LOADING_METHOD_ERROR:
				bitmap = getBitmapWithErrorMethod();
				break;
			case LOADING_METHOD_CORRECT:
				bitmap = getBitmapWithCorrectMethod();
				break;
			default:
				break;
			}
			return bitmap;
		}

		@Override
		protected void onPostExecute(Bitmap result)
		{
			super.onPostExecute(result);
			imgDisplay.setImageBitmap(result);
			if (null == result)
			{
				txtChat.setText(getString(R.string.chat_error));
				// 已知这张图片足够小，所以不需要采用抽样 Bitmap 的显示方式
				imgDisplay.setImageResource(R.drawable.img_display_load_error);
			}
			else
			{
				txtChat.setText(getString(R.string.chat_correct));
			}
		}

		/**
		 * 错误的获取方式
		 * 
		 * @return
		 */
		private Bitmap getBitmapWithErrorMethod()
		{
			Bitmap bitmap = null;
			try
			{
				bitmap = BitmapFactory.decodeResource(getResources(), RES_ID);
			}
			catch (OutOfMemoryError e)
			{
				e.printStackTrace();
				//Log.d(this.getClass().getSimpleName(), "i've known it ...");
				LogUtils.showLog(MainActivity.this, "i've known it ... " + e.toString());
			}
			return bitmap;
		}

		/**
		 * 正确的获取方式
		 * 
		 * @return
		 */
		private Bitmap getBitmapWithCorrectMethod()
		{
			Bitmap bitmap = null;
			try
			{
				bitmap = ImageUtils.getBitmapFitToScreenFromResource(MainActivity.this, getResources(), RES_ID);
			}
			catch (OutOfMemoryError e)
			{
				e.printStackTrace();
				//Log.d(this.getClass().getSimpleName(), "how could this possible ...");
				LogUtils.showLog(MainActivity.this, "how could this possible ... " + e.toString());
			}
			return bitmap;
		}

	}

	
	
}
